﻿//Setting ViewModel  
//author: Alexis 2011-06-19
//blog: http://www.cnblogs.com/alexis/


using MvvmLight;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using AccountHelper.Model;
using System.Windows.Media;
using System.Windows;
using System.Windows.Media.Imaging;
using System;
using AccountHelper.Common;
using MvvmLight.Command;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;

namespace AccountHelper.ViewModel
{

    public class SettingViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the SettingViewModel class.
        /// </summary>
        public SettingViewModel()
        {
        }

        private string userName;
        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
                if (!string.IsNullOrEmpty(value))
                {
                    UserInfo.userInfo.UserName = value;
                    ViewModelLocator.Instance.MainVM.CurrentUser.UserName = value;
                }
                RaisePropertyChanged(() => UserName);
            }
        }

        private int settingsSelectIndex;
        public int SettingsSelectIndex
        {
            get
            {
                return settingsSelectIndex;
            }
            set
            {
                settingsSelectIndex = value;
                RaisePropertyChanged("SettingsSelectIndex");
            }
        }

        #region Command
        private RelayCommand _chooseProfileImageCommand;
        public RelayCommand ChooseProfileImageCommand
        {
            get
            {
                return _chooseProfileImageCommand ?? (_chooseProfileImageCommand = new RelayCommand(() =>
                    {
                        
                        try
                        {
                            PhotoChooserTask task = new PhotoChooserTask
                            {
                                PixelHeight = 140,
                                PixelWidth = 140,
                            };
                            task.Completed += (sender, e) =>
                                {
                                    if (e.TaskResult== TaskResult.OK)
                                    {
                                        //ToDo: save to isolate storage and update profile Image uri
                                        byte[] imageBytes = new byte[e.ChosenPhoto.Length];
                                        e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

                                        var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
                                        if (!isoFile.DirectoryExists("Shared/ShellContent/tiles"))
                                        {
                                            isoFile.CreateDirectory("Shared/ShellContent/tiles");
                                        }
                                        string filePath = "/Shared/ShellContent/tiles/Profile.jpg";
                                        if (isoFile.FileExists(filePath))
                                        {
                                            isoFile.DeleteFile(filePath);
                                        }
                                        using (var stream = isoFile.CreateFile(filePath))
                                        {
                                            stream.Write(imageBytes, 0, imageBytes.Length);
                                            ViewModelLocator.Instance.MainVM.ProfileImage.SetSource(stream);
                                        }
                                        UserInfo.userInfo.ProfileImageUri = new Uri(filePath, UriKind.RelativeOrAbsolute);
                                        
                                    }
                                };
                            task.Show();
                        }
                        catch (Exception)
                        {
                            
                            throw;
                        }
                    }));
            }
        }

        #endregion

    }
}